package com.cash.channel.svc.service.common;

import com.cash.channel.api.util.ValidatorUtil;
import com.cash.channel.svc.logic.BankCardLogic;
import com.cash.channel.svc.logic.ProtocolLogic;
import com.cash.channel.svc.logic.ThirdCommonLogic;
import com.cash.channel.svc.logic.UserLogic;
import com.cash.channel.svc.service.base.BankCardBind;
import com.alibaba.fastjson.JSON;
import com.cash.channel.api.constant.Constant;
import com.cash.channel.api.exception.CashChannelException;
import com.cash.channel.api.model.ApiRequestModel;
import com.cash.channel.api.model.ApiResponseModel;
import com.cash.channel.api.model.enums.BankCardBindStatusEnum;
import com.cash.channel.api.model.request.BankCardBindRequest;
import com.cash.channel.api.model.response.BankCardBindResponse;
import com.cash.channel.api.model.user.ThirdIdModel;
import com.cash.user.model.UserInfoModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Slf4j
@Service(Constant.CHANNEL.COMMON+ Constant.SERVICE.绑卡)
public class CommonBankCardBind implements BankCardBind {

    @Autowired
    private UserLogic userLogic;

    @Autowired
    private ThirdCommonLogic thirdCommonLogic;

    @Autowired
    private BankCardLogic bankCardLogic;

    @Autowired
    private ProtocolLogic protocolLogic;

    @Override
    public ApiResponseModel<BankCardBindResponse> doBankCardBind(ApiRequestModel apiRequestModel) {
        try {
            BankCardBindRequest bankCardBindRequest = JSON.parseObject(apiRequestModel.getData(), BankCardBindRequest.class);
            if (!ValidatorUtil.isMobile(bankCardBindRequest.getReserveMobile())) {
                String phoneMessage = "预留手机号[" + bankCardBindRequest.getReserveMobile() + "]格式错误";
                log.info(phoneMessage);
                throw new CashChannelException(phoneMessage);
            }
            /*根据uid获取用户数据*/
            ThirdIdModel thirdIdModel = thirdCommonLogic.findThirdIdModelByThirdOrderId(bankCardBindRequest.getOrderNo(), apiRequestModel.getChannel());
            UserInfoModel userInfoModel = userLogic.findUserInfoModelByUid(thirdIdModel.getCUserUid());
            checkCardUserInfo(userInfoModel, bankCardBindRequest);
            /*确认操作类型，1、绑新卡，2、换默认卡*/
            if (2==bankCardBindRequest.getBusiType()) {
                /*判断是否存在*/
                if (!bankCardLogic.checkCardExist(bankCardBindRequest.getIdNo(), bankCardBindRequest.getName(), bankCardBindRequest.getCardNo(),
                        bankCardBindRequest.getReserveMobile(), bankCardBindRequest.getBankCode())) {
                    throw new CashChannelException("更换银行卡失败[银行卡不存在]");
                }
                bankCardLogic.setChannelDefaultCard(bankCardBindRequest.getCardNo(), thirdIdModel.getCUserUid(), apiRequestModel.getChannel());
            } else if (1==bankCardBindRequest.getBusiType()) {
                /*判断是否需要签约*/
                Map<String, Object> objectMap = protocolLogic.signingQueryChannel(bankCardBindRequest.getName(), bankCardBindRequest.getIdNo(), "JKJR",
                        bankCardBindRequest.getCardNo(), bankCardBindRequest.getReserveMobile(), bankCardBindRequest.getBankCode(),
                        apiRequestModel.getChannel()+Constant.ProConstant.SMS_UNIQUECODE);
                Boolean needMakeProtocol = MapUtils.getBooleanValue(objectMap, "needMakeProtocol");
                if (needMakeProtocol) {
                    return new ApiResponseModel<>(true ,0, "", new BankCardBindResponse(BankCardBindStatusEnum.SMS_SUCCESS.getStatus(), ""));
                } else {
                    /*新卡存入卡包*/
                    if (!bankCardLogic.checkCardExist(bankCardBindRequest.getIdNo(), bankCardBindRequest.getName(), bankCardBindRequest.getCardNo(),
                            bankCardBindRequest.getReserveMobile(), bankCardBindRequest.getBankCode())) {
                        bankCardLogic.addChannelBankCardToPack(thirdIdModel.getCUserUid(), apiRequestModel.getChannel(),
                                bankCardBindRequest.getBankCode(), bankCardBindRequest.getBankName(), bankCardBindRequest.getCardNo(),
                                bankCardBindRequest.getIdNo(), bankCardBindRequest.getName(), bankCardBindRequest.getReserveMobile());
                    } else {
                        bankCardLogic.setChannelDefaultCard(bankCardBindRequest.getCardNo(), thirdIdModel.getCUserUid(), apiRequestModel.getChannel());
                    }
                    /*设置默认卡*/
                    bankCardLogic.updateFundCard(thirdIdModel.getCWdId() ,bankCardBindRequest.getName(), bankCardBindRequest.getIdNo(),
                            bankCardBindRequest.getCardNo(), bankCardBindRequest.getBankCode(), bankCardBindRequest.getReserveMobile());
                    bankCardLogic.feedBackBindCard(bankCardBindRequest.getOrderNo(), 1, bankCardBindRequest.getBankCode(),
                            bankCardBindRequest.getBankName(), bankCardBindRequest.getCardNo(), bankCardBindRequest.getReserveMobile(),
                            "", apiRequestModel.getChannel(), true);
                }
            } else {
                return new ApiResponseModel<>(false, -1, "绑卡失败", new BankCardBindResponse(BankCardBindStatusEnum.FAILED.getStatus(), "不支持的[busType]"));
            }
            return new ApiResponseModel<>(true, 0, "", new BankCardBindResponse(BankCardBindStatusEnum.SUCCESS.getStatus(), ""));
        } catch (CashChannelException e) {
            log.error(e.getErrMessage(), e);
            return new ApiResponseModel<>(false, -1, "绑卡失败", new BankCardBindResponse(BankCardBindStatusEnum.FAILED.getStatus(), e.getErrMessage()));
        }
    }

    private void checkCardUserInfo(UserInfoModel userInfoModel, BankCardBindRequest bankCardBindRequest) throws CashChannelException {
        if (!bankCardBindRequest.getName().equals(userInfoModel.getcName()) || !bankCardBindRequest.getIdNo().equals(userInfoModel.getcIdno())) {
            throw new CashChannelException("姓名身份证与订单的信息不符");
        }
    }

}
